package com.hzau.tourist_service_sys.comtroller;

import com.hzau.tourist_service_sys.pojo.Token;
import com.hzau.tourist_service_sys.service.UserService;
import com.hzau.tourist_service_sys.pojo.User;
import com.hzau.tourist_service_sys.result.Result;
import com.hzau.tourist_service_sys.ultils.jjwttool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("/register")
    public Result insert(@RequestBody User user){
//        System.out.println(user);
        return userService.insert(user);
    }
    @GetMapping("/find/{id}")
    public Result findById(@PathVariable Long id){
        return userService.findById(id);
    }
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        String username=user.getUserName();
        String password=user.getPassword();
        System.out.println(username);
        System.out.println(password);
        User user1=userService.findByNameandPwd(username,password);
        if (user1==null){
            return new Result(305,"账户或密码出错,请重新登录",null);
        }
        String JWT=jjwttool.genJwt(user1.getUserId(),user1.getTag());
        Token token=new Token();
        token.setToken(JWT);
        token.setTag(user1.getTag());
        return Result.success(token);
    }

}
